package com.ergwang.dao;

import com.ergwang.Model.Goods;
import com.ergwang.Model.Spec;
import com.ergwang.Model.Type;
import com.ergwang.Model.admin.bo.GoodsInfoBO;
import com.ergwang.Model.admin.bo.GoodsTypeBO;
import com.ergwang.Model.admin.bo.MassageReplyBO;
import com.ergwang.Model.admin.bo.SpecInfoBO;
import com.ergwang.Model.admin.vo.GoodsInfoVO;
import com.ergwang.Model.mall.vo.SearchGoodsVO;
import com.ergwang.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

/**
 * @author：ergwang
 * @Date: 2020/5/30
 * @Time: 15:30
 */
public class GoodsDaoImpl implements GoodsDao {
    public List<Type> getType() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Type> query = null;
        try {
            query = runner.query("select * from type",
                    new BeanListHandler<Type>(Type.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    public List<Goods> getGoodsByType(String typeId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Goods> query = null;
        try {
            query = runner.query("select * from goods where typeid = ?",
                    new BeanListHandler<Goods>(Goods.class),
                    typeId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    public int deleteGoods(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        int delete = 0;
        try {
            delete = runner.update("delete from goods where id = ?", id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return delete;
    }

    public int addType(GoodsTypeBO goodsTypeBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        int add = 0;
        try {
            add = runner.update("insert into type (name) values (?)",
                    goodsTypeBO.getName());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return add;
    }

    public void addGoods(Goods goods) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            int addGoods = runner.update("insert into goods values (null ,?,?,?,?,?,?)",
                    goods.getImg(),
                    goods.getName(),
                    goods.getPrice(),
                    goods.getTypeid(),
                    goods.getStockNum(),
                    goods.getDesc());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    /**
     * 获取最近添加的商品的id 返会到service层
     * @param name
     * @return
     */
    public int lastInsertId(String name) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        // 调用sql的方法，获取最近添加数据的id，（线程安全的）
        BigInteger query = null;
        try {
            query = runner.query("select last_insert_id()",
                    new ScalarHandler<BigInteger>());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        // 返回值将biginteget转换为int
        return query.intValue();
    }

    public void addSpecs(List<Spec> specs) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        for (Spec spec : specs){
            try {
                runner.update("insert into spec values (null , ?, ?, ?, ?)",
                        spec.getSpecName(),
                        spec.getStockNum(),
                        spec.getUnitPrice(),
                        spec.getGoodsId());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public List<SpecInfoBO> getGoodsInfo_specs(GoodsInfoVO goodsInfoVO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<SpecInfoBO> specs = null;
        try {
            specs = runner.query("select * from spec where goodsId = ?",
                    new BeanListHandler<SpecInfoBO>(SpecInfoBO.class),
                    goodsInfoVO.getGoods().getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return specs;
    }

    public GoodsInfoBO getGoodsInfo_goods(GoodsInfoVO goodsInfoVO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        GoodsInfoBO goods = null;
        try {
            goods = runner.query("select * from goods where id = ?",
                    new BeanHandler<GoodsInfoBO>(GoodsInfoBO.class),
                    goodsInfoVO.getGoods().getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return goods;
    }

    public Spec getSpecs(Integer goodsId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Spec query = null;
        try {
            query = runner.query("select * from spec where goodsId = ?",
                    new BeanHandler<Spec>(Spec.class),
                    goodsId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    public void deleteSpec(Spec deleteSpec) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("delete from spec where specName = ? and goodsId = ?",
                    deleteSpec.getSpecName(),
                    deleteSpec.getGoodsId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void updateGoods(Goods goods) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update goods set img=?,name =?,price=?,typeid=?,stockNum=?,desc=? where id = ?",
                    goods.getImg(),goods.getName(),goods.getPrice(),goods.getTypeid(),
                    goods.getStockNum(),goods.getDesc(),goods.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void deleteType(String typeId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("delete from type where id = ?",typeId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 将响应留言的回复存入数据库,并将state设为0
     * @param msgReplyBO
     */
    public void msg_reply(MassageReplyBO msgReplyBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update massages set replyContent = ?,state = 0 where id = ?",
                    msgReplyBO.getContent(),msgReplyBO.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 根据keyword模糊查询商品信息并返回
     * @param keyword
     * @return
     */
    public List<SearchGoodsVO> searchGoods(String keyword) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<SearchGoodsVO> query = null;
        try {
            query = runner.query("select id,img,name,price,typeid as typeId from goods where name like ?",
                    new BeanListHandler<SearchGoodsVO>(SearchGoodsVO.class),
                    "%"+keyword+"%");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }
}
